﻿using CarPCBATest.Procedure;
using Common;
using ImageSourceModuleCs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using VM.Core;

namespace CarPCBATest.ViewModel
{
    public partial class HomeViewModel
    {
        public bool A29Common(LedCurrentConfig config, int chl)
        {
            if (chl == 0)
            {
                IOModule.SetDOValue(DODefine.Power, 1);
            }
            else if (chl == 1)
            {
                IOModule.SetDOValue(DODefine.Power2, 1);
            }
            else
            {
                throw new Exception("未定义的电源通道");
            }
            Log = $"电源[{chl + 1}]ON";
            Thread.Sleep(500);
            currentProcId = config.ProcID;
            Log = $"开始测试{config.LedDesc}功能";

            bool comRet = true;
            Thread.Sleep(500);
            // 1.4测试电流
            double current = 0;
            try
            {
                current = DM3058E.GetCurrent() * 1000;
            }
            catch
            {
                current = DM3058E.GetCurrent() * 1000;
            }

            Log = $"功能{config.LedDesc}电流为{current}mA";
            LocalLogs.Add(Log);
            if (current <= config.UpperLimitCurrent & current >= config.LowerLimitCurrent)
            {
                // ok
            }
            else
            {
                //ng
                Log = $"功能{config.LedDesc}电流不在OK范围";
                LocalLogs.Add(Log);
                foreach (var item in TestResultDatas)
                {
                    item.IsOK = false;
                    item.NgMsg.Add($"功能[{config.LedDesc}] 电流=[{current}] 不在OK范围");
                }
                comRet = false;
            }
            bool timeOut = false;
            vmProcHaveExec = false;
            swGlobal.Restart();
            vmResult = null;
            Application.Current.Dispatcher.Invoke(() =>
            {
                VisionTool.SetProcVisionRet(config.ProcName, control);
            });
            // 2.视觉检测

            // sdk获取图像
            CameraTool.Open();
            var image = CameraTool.GetCurrentImage();
            //设置到图像源
            ImageSourceModuleTool imageSourceModuleTool = (ImageSourceModuleTool)VmSolution.Instance[$"{config.ProcName}.图像源1"];
            imageSourceModuleTool.SetImageData(image);

            VisionTool.RunProc(config.ProcName);
            Task.Run(() =>
            {
                Thread.Sleep(3000);
                timeOut = true;
            });
            while (!timeOut)
            {
                if (vmProcHaveExec)
                {
                    break;
                }
                Thread.Sleep(10);
            }
            if (timeOut)
            {
                // 超时
                Log = "视觉判定流程执行超时";
                foreach (var item in TestResultDatas)
                {
                    item.IsOK = false;
                    item.NgMsg.Add($"功能[{config.LedDesc}] 视觉判定流程执行超时");
                }
                return false;
            }
            if (vmResult is null)
            {
                Log = "视觉判定测试结果为null";
                foreach (var item in TestResultDatas)
                {
                    item.IsOK = false;
                    item.NgMsg.Add($"功能[{config.LedDesc}] 视觉判定测试结果为null");
                }
                return false;
            }
            Mylog.Info($"vmResult=[{vmResult}]");

            if (vmResult == null)
            {
                comRet = false;
                Log = "灯光视觉流程执行返回Null";
                foreach (var item in TestResultDatas)
                {
                    item.IsOK = false;
                    item.NgMsg.Add($"功能[{config.LedDesc}] 灯光视觉流程执行返回Null");
                }
                return false;
            }
            else
            {
                var tmp = vmResult.Split(',');

                if (tmp.Count() != 6)
                {
                    comRet = false;
                    Log = "灯光视觉流程执行返回值异常";
                    for (int i = 0; i < config.StationCount; i++)
                    {
                        TestResultDatas[i].IsOK = false;
                        TestResultDatas[i].NgMsg.Add($"功能[{config.LedDesc}] 灯光视觉流程执行返回值异常");
                    }
                }
                else
                {
                    for (int i = 0; i < config.StationCount; i++)
                    {
                        if (tmp[i] == "1")
                        {
                            Log = $"工位[{i + 1}]功能[{config.LedDesc}]视觉判定OK";
                            LocalLogs.Add(Log);
                            TestResultDatas[i].IsOK = true & TestResultDatas[i].IsOK;
                        }
                        else
                        {
                            Log = $"工位[{i + 1}]功能[{config.LedDesc}]视觉判定NG";
                            LocalLogs.Add(Log);
                            TestResultDatas[i].IsOK = false && TestResultDatas[i].IsOK;
                            TestResultDatas[i].NgMsg.Add("视觉判定NG");
                            comRet = false;
                        }
                    }
                }
            }
            if (chl == 0)
            {
                IOModule.SetDOValue(DODefine.Power, 0);
            }
            else if (chl == 1)
            {
                IOModule.SetDOValue(DODefine.Power2, 0);
            }
            else
            {
                throw new Exception("未定义的电源通道");
            }
            Log = $"电源[{chl + 1}]OFF";
            return comRet;
        }

        public bool A29Ch1(LedCurrentConfig config)
        {
            return A29Common(config, 0);
        }

        public bool A29Ch2(LedCurrentConfig config)
        {
            return A29Common(config, 1);
        }
    }
}